package com.shenyi.mould.rest.controller.wx;

import com.shenyi.common.api.ISysSmsLogService;
import com.shenyi.common.entity.SysSmsLogDO;
import com.shenyi.core.utils.*;
import com.shenyi.wx.annotation.IgnoreAuth;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author richard.fan
 * @version 1.0
 * @date 2018/11/07
 * @desc 短信
 * @see
 * @since 1.0
 */
@RestController
@RequestMapping("api")
public class ApiSmsController {

    @Autowired
    private ISysSmsLogService smsLogService;

    /**
     * 发送短信
     *
     * @param request request
     * @param params 请求参数{mobile：电话号码字符串，中间用英文逗号间隔,content：内容字符串,stime：追加发送时间，可为空，为空为及时发送}
     * @return R
     */
    @IgnoreAuth
    @RequestMapping("/sendSms")
    public R sendSms(HttpServletRequest request, @RequestParam Map<String, String> params) {
        SysSmsLogDO smsLog = new SysSmsLogDO();
        String validIP = RequestUtil.getIpAddrByRequest(request);
        if (ResourceUtil.getConfigByName("sms.validIp").indexOf(validIP) < 0) {
            throw new RRException("非法IP请求！");
        }
        smsLog.setMobile(params.get("mobile"));
        smsLog.setContent(params.get("content"));
        String stime = params.get("stime");
        if (StringUtils.isNotEmpty(stime)) {
            smsLog.setStime(DateUtils.convertStringToDate(stime));
        }
        SysSmsLogDO SysSmsLogDO = smsLogService.sendSms(smsLog);
        return R.ok().put("result", SysSmsLogDO);
    }
}
